package cn.zane.mybatis.binding;

import cn.zane.mybatis.session.SqlSession;

import java.io.Serial;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author zane
 * @date 2025/01/17
 * @Description 映射器代理
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {

    @Serial
    private static final long serialVersionUID = -412638163612681749L;
    private SqlSession sqlSession;
    private final Class<T> mapperInterface;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // Object 提供的 toString、hashCode 等方法是不需要代理执行的。所以这里需要进行判断
        if (Object.class.equals(method.getDeclaringClass())) {
            // 调用真实方法
            return method.invoke(this, args);
        } else {
            // 接口名称+方法名称
            String dbAction = mapperInterface.getName() + "." + method.getName();
            return "你的被代理了！方法:" + method.getName() + " 入参:" + args;
        }
    }
}
